<template>
  <div class="storage-optimization-view">
    <!-- 页面头部 -->
    <div class="page-header glass-card">
      <div class="header-content">
        <div class="header-left">
          <button class="back-btn" @click="goBack">
            <svg fill="none" viewBox="0 0 24 24">
              <path d="M19 12H5M12 19l-7-7 7-7" stroke="currentColor" stroke-width="2"/>
            </svg>
            返回
          </button>
        </div>

        <div class="header-info">
          <h1 class="page-title">
            <svg fill="none" viewBox="0 0 24 24">
              <path d="M22 12h-4l-3 9L9 3l-3 9H2" stroke="currentColor" stroke-width="2"/>
            </svg>
            存储优化与CDN
          </h1>
          <p class="page-description">优化媒体文件存储策略，加速全球访问体验</p>
        </div>

        <div class="header-actions">
          <button :disabled="isAnalyzing" class="action-btn scan" @click="startStorageAnalysis">
            <svg v-if="!isAnalyzing" fill="none" viewBox="0 0 24 24">
              <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" stroke="currentColor" stroke-width="2"/>
            </svg>
            <svg v-else class="spinner" fill="none" viewBox="0 0 24 24">
              <circle cx="12" cy="12" fill="none" r="10" stroke="currentColor" stroke-dasharray="31.416" stroke-dashoffset="31.416" stroke-width="4"/>
            </svg>
            {{ isAnalyzing ? '分析中...' : '存储分析' }}
          </button>
          <button class="action-btn optimize" @click="showOptimizationDialog">
            <svg fill="none" viewBox="0 0 24 24">
              <path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z" stroke="currentColor" stroke-width="2"/>
            </svg>
            批量优化
          </button>
        </div>
      </div>
    </div>

    <!-- 存储概览 -->
    <div class="storage-overview glass-card">
      <div class="overview-grid">
        <div class="overview-item total-storage">
          <div class="item-icon">
            <svg fill="none" viewBox="0 0 24 24">
              <rect height="16" rx="2" stroke="currentColor" stroke-width="2" width="16" x="4" y="4"/>
              <rect height="6" stroke="currentColor" stroke-width="2" width="6" x="9" y="9"/>
            </svg>
          </div>
          <div class="item-content">
            <h3>总存储空间</h3>
            <div class="storage-bar">
              <div :style="{ width: (storageStats.used / storageStats.total) * 100 + '%' }" class="storage-used"></div>
            </div>
            <div class="storage-details">
              <span class="used">{{ formatFileSize(storageStats.used) }}</span>
              <span class="separator">/</span>
              <span class="total">{{ formatFileSize(storageStats.total) }}</span>
            </div>
          </div>
        </div>

        <div class="overview-item cdn-hits">
          <div class="item-icon">
            <svg fill="none" viewBox="0 0 24 24">
              <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/>
              <path d="M8 12l2 2 4-4" stroke="currentColor" stroke-width="2"/>
            </svg>
          </div>
          <div class="item-content">
            <h3>CDN命中率</h3>
            <div class="metric-value">{{ (cdnStats.hitRate * 100).toFixed(1) }}%</div>
            <div :class="cdnStats.trend" class="metric-trend">
              <svg fill="none" viewBox="0 0 24 24">
                <path d="M7 17l5-5 5 5M7 7l5 5 5-5" stroke="currentColor" stroke-width="2"/>
              </svg>
              +{{ cdnStats.improvement }}%
            </div>
          </div>
        </div>

        <div class="overview-item bandwidth">
          <div class="item-icon">
            <svg fill="none" viewBox="0 0 24 24">
              <path d="M3 12a9 9 0 1 0 18 0 9 9 0 1 0-18 0" stroke="currentColor" stroke-width="2"/>
              <path d="M9 12l2 2 4-4" stroke="currentColor" stroke-width="2"/>
            </svg>
          </div>
          <div class="item-content">
            <h3>带宽节省</h3>
            <div class="metric-value">{{ formatFileSize(bandwidthStats.saved) }}</div>
            <div class="metric-subtitle">本月已节省</div>
          </div>
        </div>

        <div class="overview-item optimization">
          <div class="item-icon">
            <svg fill="none" viewBox="0 0 24 24">
              <path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z" stroke="currentColor" stroke-width="2"/>
            </svg>
          </div>
          <div class="item-content">
            <h3>优化潜力</h3>
            <div class="metric-value">{{ formatFileSize(optimizationStats.potential) }}</div>
            <div class="metric-subtitle">可释放空间</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content">
      <div class="content-grid">
        <!-- 左侧：存储分析 -->
        <div class="analysis-section">
          <!-- 存储分布 -->
          <div class="storage-distribution glass-card">
            <div class="card-header">
              <h3 class="card-title">
                <svg fill="none" viewBox="0 0 24 24">
                  <path d="M9 19c-4.3 1.4-4.3-2.5-6-3m12 5v-3.5c0-1 .1-1.4-.5-2 2.8-.3 5.5-1.3 5.5-6a4.6 4.6 0 00-1.3-3.2 4.2 4.2 0 00-.1-3.2s-1.1-.3-3.5 1.3a12.3 12.3 0 00-6.2 0C6.5 2.8 5.4 3.1 5.4 3.1a4.2 4.2 0 00-.1 3.2A4.6 4.6 0 004 9.5c0 4.7 2.7 5.7 5.5 6-.6.6-.6 1.2-.5 2V21" stroke="currentColor" stroke-width="2"/>
                </svg>
                存储分布
              </h3>
              <div class="card-actions">
                <button class="refresh-btn" @click="refreshStorageData">
                  <svg fill="none" viewBox="0 0 24 24">
                    <polyline points="23 4 23 10 17 10" stroke="currentColor" stroke-width="2"/>
                    <polyline points="1 20 1 14 7 14" stroke="currentColor" stroke-width="2"/>
                    <path d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15" stroke="currentColor" stroke-width="2"/>
                  </svg>
                </button>
              </div>
            </div>
            <div class="card-content">
              <div class="distribution-chart">
                <div
                  v-for="category in storageDistribution"
                  :key="category.type"
                  :style="{
                    '--size': category.percentage + '%',
                    '--color': category.color
                  }"
                  class="chart-segment"
                >
                  <div class="segment-info">
                    <div class="segment-label">{{ category.label }}</div>
                    <div class="segment-size">{{ formatFileSize(category.size) }}</div>
                    <div class="segment-percentage">{{ category.percentage.toFixed(1) }}%</div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 重复文件检测 -->
          <div class="duplicate-detection glass-card">
            <div class="card-header">
              <h3 class="card-title">
                <svg fill="none" viewBox="0 0 24 24">
                  <rect height="13" rx="2" ry="2" stroke="currentColor" stroke-width="2" width="13" x="9" y="9"/>
                  <path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1" stroke="currentColor" stroke-width="2"/>
                </svg>
                重复文件检测
              </h3>
              <span class="duplicate-count">发现 {{ duplicateFiles.length }} 组重复</span>
            </div>
            <div class="card-content">
              <div class="duplicate-list">
                <div
                  v-for="group in duplicateFiles.slice(0, 5)"
                  :key="group.id"
                  class="duplicate-item"
                  @click="viewDuplicateGroup(group)"
                >
                  <div class="duplicate-preview">
                    <img v-if="group.thumbnail" :alt="group.name" :src="group.thumbnail" />
                    <div v-else :class="group.type" class="file-icon">
                      <svg fill="none" viewBox="0 0 24 24">
                        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" stroke="currentColor" stroke-width="2"/>
                      </svg>
                    </div>
                  </div>
                  <div class="duplicate-info">
                    <h4 class="duplicate-name">{{ group.name }}</h4>
                    <div class="duplicate-meta">
                      <span class="file-count">{{ group.count }} 个文件</span>
                      <span class="potential-saving">可节省 {{ formatFileSize(group.wasteSize) }}</span>
                    </div>
                  </div>
                  <div class="duplicate-action">
                    <button class="quick-clean-btn" @click.stop="quickCleanDuplicates(group)">
                      <svg fill="none" viewBox="0 0 24 24">
                        <polyline points="3,6 5,6 21,6" stroke="currentColor" stroke-width="2"/>
                        <path d="M19,6v14a2,2 0 0,1 -2,2H7a2,2 0 0,1 -2,-2V6m3,0V4a2,2 0 0,1 2,-2h4a2,2 0 0,1 2,2v2" stroke="currentColor" stroke-width="2"/>
                      </svg>
                    </button>
                  </div>
                </div>
                <button v-if="duplicateFiles.length > 5" class="view-all-btn" @click="viewAllDuplicates">
                  查看全部 {{ duplicateFiles.length }} 组重复文件
                </button>
              </div>
            </div>
          </div>

          <!-- 压缩建议 -->
          <div class="compression-suggestions glass-card">
            <div class="card-header">
              <h3 class="card-title">
                <svg fill="none" viewBox="0 0 24 24">
                  <path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4M7 10l5 5 5-5M12 15V3" stroke="currentColor" stroke-width="2"/>
                </svg>
                压缩建议
              </h3>
            </div>
            <div class="card-content">
              <div class="suggestions-list">
                <div
                  v-for="suggestion in compressionSuggestions"
                  :key="suggestion.id"
                  :class="suggestion.priority"
                  class="suggestion-item"
                >
                  <div :class="suggestion.priority" class="suggestion-icon">
                    <svg v-if="suggestion.priority === 'high'" fill="none" viewBox="0 0 24 24">
                      <path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z" stroke="currentColor" stroke-width="2"/>
                      <line stroke="currentColor" stroke-width="2" x1="12" x2="12" y1="9" y2="13"/>
                      <line stroke="currentColor" stroke-width="2" x1="12" x2="12.01" y1="17" y2="17"/>
                    </svg>
                    <svg v-else-if="suggestion.priority === 'medium'" fill="none" viewBox="0 0 24 24">
                      <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/>
                      <line stroke="currentColor" stroke-width="2" x1="12" x2="12" y1="8" y2="12"/>
                      <line stroke="currentColor" stroke-width="2" x1="12" x2="12.01" y1="16" y2="16"/>
                    </svg>
                    <svg v-else fill="none" viewBox="0 0 24 24">
                      <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" stroke="currentColor" stroke-width="2"/>
                      <polyline points="22,4 12,14.01 9,11.01" stroke="currentColor" stroke-width="2"/>
                    </svg>
                  </div>
                  <div class="suggestion-content">
                    <h4 class="suggestion-title">{{ suggestion.title }}</h4>
                    <p class="suggestion-description">{{ suggestion.description }}</p>
                    <div class="suggestion-savings">
                      可节省空间: <strong>{{ formatFileSize(suggestion.potentialSavings) }}</strong>
                    </div>
                  </div>
                  <button class="apply-suggestion-btn" @click="applySuggestion(suggestion)">
                    应用建议
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧：CDN管理 -->
        <div class="cdn-section">
          <!-- CDN状态 -->
          <div class="cdn-status glass-card">
            <div class="card-header">
              <h3 class="card-title">
                <svg fill="none" viewBox="0 0 24 24">
                  <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/>
                  <line stroke="currentColor" stroke-width="2" x1="2" x2="6" y1="12" y2="12"/>
                  <line stroke="currentColor" stroke-width="2" x1="18" x2="22" y1="12" y2="12"/>
                  <line stroke="currentColor" stroke-width="2" x1="12" x2="12" y1="2" y2="6"/>
                  <line stroke="currentColor" stroke-width="2" x1="12" x2="12" y1="18" y2="22"/>
                </svg>
                CDN节点状态
              </h3>
              <div class="cdn-toggle">
                <label class="toggle-label">
                  CDN加速
                  <input v-model="cdnEnabled" type="checkbox" @change="toggleCDN" />
                  <span class="toggle-slider"></span>
                </label>
              </div>
            </div>
            <div class="card-content">
              <div class="cdn-nodes">
                <div
                  v-for="node in cdnNodes"
                  :key="node.region"
                  :class="node.status"
                  class="node-item"
                >
                  <div class="node-region">{{ node.region }}</div>
                  <div class="node-latency">{{ node.latency }}ms</div>
                  <div :class="node.status" class="node-status">
                    <span class="status-dot"></span>
                    {{ node.status === 'healthy' ? '正常' : node.status === 'warning' ? '警告' : '异常' }}
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 缓存管理 -->
          <div class="cache-management glass-card">
            <div class="card-header">
              <h3 class="card-title">
                <svg fill="none" viewBox="0 0 24 24">
                  <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" stroke="currentColor" stroke-width="2"/>
                </svg>
                缓存管理
              </h3>
            </div>
            <div class="card-content">
              <div class="cache-stats">
                <div class="cache-metric">
                  <span class="metric-label">缓存大小</span>
                  <span class="metric-value">{{ formatFileSize(cacheStats.totalSize) }}</span>
                </div>
                <div class="cache-metric">
                  <span class="metric-label">缓存文件</span>
                  <span class="metric-value">{{ cacheStats.fileCount.toLocaleString() }}</span>
                </div>
                <div class="cache-metric">
                  <span class="metric-label">命中率</span>
                  <span class="metric-value">{{ (cacheStats.hitRate * 100).toFixed(1) }}%</span>
                </div>
              </div>
              <div class="cache-actions">
                <button class="cache-action-btn" @click="refreshCDNCache">
                  <svg fill="none" viewBox="0 0 24 24">
                    <polyline points="23 4 23 10 17 10" stroke="currentColor" stroke-width="2"/>
                    <polyline points="1 20 1 14 7 14" stroke="currentColor" stroke-width="2"/>
                    <path d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15" stroke="currentColor" stroke-width="2"/>
                  </svg>
                  刷新缓存
                </button>
                <button class="cache-action-btn" @click="preloadCommonFiles">
                  <svg fill="none" viewBox="0 0 24 24">
                    <path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4M7 10l5 5 5-5M12 15V3" stroke="currentColor" stroke-width="2"/>
                  </svg>
                  预加载
                </button>
                <button class="cache-action-btn danger" @click="clearCDNCache">
                  <svg fill="none" viewBox="0 0 24 24">
                    <polyline points="3,6 5,6 21,6" stroke="currentColor" stroke-width="2"/>
                    <path d="M19,6v14a2,2 0 0,1 -2,2H7a2,2 0 0,1 -2,-2V6m3,0V4a2,2 0 0,1 2,-2h4a2,2 0 0,1 2,2v2" stroke="currentColor" stroke-width="2"/>
                  </svg>
                  清空缓存
                </button>
              </div>
            </div>
          </div>

          <!-- 访问分析 -->
          <div class="access-analytics glass-card">
            <div class="card-header">
              <h3 class="card-title">
                <svg fill="none" viewBox="0 0 24 24">
                  <path d="M18 20V10M12 20V4M6 20v-6" stroke="currentColor" stroke-width="2"/>
                </svg>
                访问分析
              </h3>
              <div class="time-range-selector">
                <select v-model="selectedTimeRange" @change="updateAnalytics">
                  <option value="1d">24小时</option>
                  <option value="7d">7天</option>
                  <option value="30d">30天</option>
                </select>
              </div>
            </div>
            <div class="card-content">
              <div class="analytics-summary">
                <div class="summary-item">
                  <div class="summary-value">{{ analyticsData.totalRequests.toLocaleString() }}</div>
                  <div class="summary-label">总请求数</div>
                </div>
                <div class="summary-item">
                  <div class="summary-value">{{ formatFileSize(analyticsData.totalTraffic) }}</div>
                  <div class="summary-label">总流量</div>
                </div>
                <div class="summary-item">
                  <div class="summary-value">{{ analyticsData.avgResponseTime }}ms</div>
                  <div class="summary-label">平均响应时间</div>
                </div>
              </div>
              <div class="top-files">
                <h4>热门文件</h4>
                <div class="file-list">
                  <div
                    v-for="file in analyticsData.topFiles.slice(0, 5)"
                    :key="file.id"
                    class="file-item"
                  >
                    <div class="file-name">{{ file.name }}</div>
                    <div class="file-requests">{{ file.requests }} 次</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 优化对话框 -->
    <div v-if="showOptimizationModal" class="optimization-modal" @click="closeOptimizationModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>批量优化设置</h3>
          <button class="modal-close" @click="closeOptimizationModal">
            <svg fill="none" viewBox="0 0 24 24">
              <line stroke="currentColor" stroke-width="2" x1="18" x2="6" y1="6" y2="18"/>
              <line stroke="currentColor" stroke-width="2" x1="6" x2="18" y1="6" y2="18"/>
            </svg>
          </button>
        </div>
        <div class="modal-body">
          <div class="optimization-options">
            <div class="option-group">
              <h4>压缩选项</h4>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.compressImages" type="checkbox" />
                图片压缩 (JPEG质量80%, PNG无损)
              </label>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.compressVideos" type="checkbox" />
                视频压缩 (H.264编码, 适中质量)
              </label>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.generateWebP" type="checkbox" />
                生成WebP格式 (现代浏览器优化)
              </label>
            </div>
            <div class="option-group">
              <h4>清理选项</h4>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.removeDuplicates" type="checkbox" />
                删除重复文件
              </label>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.cleanThumbnails" type="checkbox" />
                清理无用缩略图
              </label>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.archiveOldFiles" type="checkbox" />
                归档超过一年的文件
              </label>
            </div>
            <div class="option-group">
              <h4>CDN选项</h4>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.enableCDN" type="checkbox" />
                启用CDN加速
              </label>
              <label class="checkbox-label">
                <input v-model="optimizationConfig.preloadPopular" type="checkbox" />
                预加载热门文件
              </label>
            </div>
          </div>
          <div class="optimization-preview">
            <div class="preview-item">
              <span class="preview-label">预计节省空间:</span>
              <span class="preview-value">{{ formatFileSize(estimatedSavings) }}</span>
            </div>
            <div class="preview-item">
              <span class="preview-label">处理文件数量:</span>
              <span class="preview-value">{{ estimatedFileCount.toLocaleString() }}</span>
            </div>
            <div class="preview-item">
              <span class="preview-label">预计完成时间:</span>
              <span class="preview-value">{{ estimatedTime }}</span>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="closeOptimizationModal">取消</button>
          <button :disabled="!hasSelectedOptions" class="btn btn-primary" @click="startBatchOptimization">
            开始优化
          </button>
        </div>
      </div>
    </div>

    <!-- 进度对话框 -->
    <div v-if="showProgressModal" class="progress-modal">
      <div class="progress-content">
        <div class="progress-header">
          <h3>批量优化进行中</h3>
          <div class="progress-stats">
            <span>{{ Math.round(optimizationProgress.percentage) }}% 完成</span>
            <span>{{ optimizationProgress.processed }} / {{ optimizationProgress.total }}</span>
          </div>
        </div>
        <div class="progress-bar">
          <div :style="{ width: optimizationProgress.percentage + '%' }" class="progress-fill"></div>
        </div>
        <div class="current-task">
          <div class="task-info">
            <span class="task-name">{{ optimizationProgress.currentTask }}</span>
            <span class="task-status">{{ optimizationProgress.status }}</span>
          </div>
          <div class="task-savings">
            已节省空间: {{ formatFileSize(optimizationProgress.savedSpace) }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Notify } from '@opentiny/vue'

const router = useRouter()

// 响应式数据
const isAnalyzing = ref(false)
const showOptimizationModal = ref(false)
const showProgressModal = ref(false)
const cdnEnabled = ref(true)
const selectedTimeRange = ref('7d')

// 存储统计
const storageStats = ref({
  used: 8.4 * 1024 * 1024 * 1024, // 8.4GB
  total: 50 * 1024 * 1024 * 1024, // 50GB
  available: 41.6 * 1024 * 1024 * 1024 // 41.6GB
})

// CDN统计
const cdnStats = ref({
  hitRate: 0.847,
  improvement: 12.3,
  trend: 'up'
})

// 带宽统计
const bandwidthStats = ref({
  saved: 2.8 * 1024 * 1024 * 1024 // 2.8GB
})

// 优化统计
const optimizationStats = ref({
  potential: 1.6 * 1024 * 1024 * 1024 // 1.6GB
})

// 存储分布
const storageDistribution = ref([
  {
    type: 'images',
    label: '图片',
    size: 4.2 * 1024 * 1024 * 1024,
    percentage: 50,
    color: '#3b82f6'
  },
  {
    type: 'videos',
    label: '视频',
    size: 2.8 * 1024 * 1024 * 1024,
    percentage: 33.3,
    color: '#ef4444'
  },
  {
    type: 'audio',
    label: '音频',
    size: 0.8 * 1024 * 1024 * 1024,
    percentage: 9.5,
    color: '#10b981'
  },
  {
    type: 'documents',
    label: '文档',
    size: 0.6 * 1024 * 1024 * 1024,
    percentage: 7.2,
    color: '#f59e0b'
  }
])

// 重复文件
const duplicateFiles = ref([
  {
    id: 1,
    name: 'IMG_2023_beach.jpg',
    count: 3,
    type: 'image',
    wasteSize: 12 * 1024 * 1024,
    thumbnail: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=300&fit=crop'
  },
  {
    id: 2,
    name: 'family_dinner.mp4',
    count: 2,
    type: 'video',
    wasteSize: 45 * 1024 * 1024,
    thumbnail: null
  },
  {
    id: 3,
    name: 'vacation_song.mp3',
    count: 4,
    type: 'audio',
    wasteSize: 8 * 1024 * 1024,
    thumbnail: null
  }
])

// 压缩建议
const compressionSuggestions = ref([
  {
    id: 1,
    title: '高分辨率图片压缩',
    description: '发现257个超高分辨率图片可以无损压缩',
    priority: 'high',
    potentialSavings: 850 * 1024 * 1024
  },
  {
    id: 2,
    title: '视频重新编码',
    description: '使用H.265编码可显著减少视频文件大小',
    priority: 'medium',
    potentialSavings: 1200 * 1024 * 1024
  },
  {
    id: 3,
    title: '生成WebP格式',
    description: '为现代浏览器生成WebP版本可提升加载速度',
    priority: 'low',
    potentialSavings: 320 * 1024 * 1024
  }
])

// CDN节点
const cdnNodes = ref([
  {
    region: '北京',
    latency: 15,
    status: 'healthy'
  },
  {
    region: '上海',
    latency: 22,
    status: 'healthy'
  },
  {
    region: '广州',
    latency: 28,
    status: 'warning'
  },
  {
    region: '成都',
    latency: 35,
    status: 'healthy'
  },
  {
    region: '香港',
    latency: 45,
    status: 'error'
  }
])

// 缓存统计
const cacheStats = ref({
  totalSize: 3.2 * 1024 * 1024 * 1024,
  fileCount: 15847,
  hitRate: 0.847
})

// 访问分析数据
const analyticsData = ref({
  totalRequests: 125847,
  totalTraffic: 15.6 * 1024 * 1024 * 1024,
  avgResponseTime: 156,
  topFiles: [
    { id: 1, name: 'family_photo_001.jpg', requests: 2847 },
    { id: 2, name: 'vacation_video.mp4', requests: 1923 },
    { id: 3, name: 'birthday_album.jpg', requests: 1756 },
    { id: 4, name: 'wedding_photos.jpg', requests: 1534 },
    { id: 5, name: 'pet_video.mp4', requests: 1287 }
  ]
})

// 优化配置
const optimizationConfig = reactive({
  compressImages: true,
  compressVideos: false,
  generateWebP: true,
  removeDuplicates: true,
  cleanThumbnails: true,
  archiveOldFiles: false,
  enableCDN: true,
  preloadPopular: true
})

// 优化进度
const optimizationProgress = ref({
  percentage: 0,
  processed: 0,
  total: 0,
  currentTask: '',
  status: '',
  savedSpace: 0
})

// 计算属性
const hasSelectedOptions = computed(() => {
  return Object.values(optimizationConfig).some(value => value)
})

const estimatedSavings = computed(() => {
  let savings = 0
  if (optimizationConfig.compressImages) savings += 850 * 1024 * 1024
  if (optimizationConfig.compressVideos) savings += 1200 * 1024 * 1024
  if (optimizationConfig.generateWebP) savings += 320 * 1024 * 1024
  if (optimizationConfig.removeDuplicates) savings += 280 * 1024 * 1024
  if (optimizationConfig.cleanThumbnails) savings += 45 * 1024 * 1024
  if (optimizationConfig.archiveOldFiles) savings += 890 * 1024 * 1024
  return savings
})

const estimatedFileCount = computed(() => {
  let count = 0
  if (optimizationConfig.compressImages) count += 257
  if (optimizationConfig.compressVideos) count += 84
  if (optimizationConfig.generateWebP) count += 257
  if (optimizationConfig.removeDuplicates) count += 156
  if (optimizationConfig.cleanThumbnails) count += 1247
  if (optimizationConfig.archiveOldFiles) count += 523
  return count
})

const estimatedTime = computed(() => {
  const filesPerMinute = 15
  const minutes = Math.ceil(estimatedFileCount.value / filesPerMinute)
  if (minutes < 60) return `${minutes} 分钟`
  const hours = Math.floor(minutes / 60)
  const remainingMinutes = minutes % 60
  return `${hours} 小时 ${remainingMinutes} 分钟`
})

// 工具函数
const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 事件处理
const goBack = () => {
  router.back()
}

const startStorageAnalysis = async () => {
  isAnalyzing.value = true
  try {
    // 模拟分析过程
    await new Promise(resolve => setTimeout(resolve, 3000))

    Notify({
      type: 'success',
      message: '存储分析完成',
      position: 'top-right'
    })

    // 刷新数据
    await refreshStorageData()

  } catch (error) {
    Notify({
      type: 'error',
      message: '存储分析失败，请重试',
      position: 'top-right'
    })
  } finally {
    isAnalyzing.value = false
  }
}

const refreshStorageData = async () => {
  // 模拟数据刷新
  await new Promise(resolve => setTimeout(resolve, 500))

  Notify({
    type: 'info',
    message: '存储数据已刷新',
    position: 'top-right'
  })
}

const showOptimizationDialog = () => {
  showOptimizationModal.value = true
}

const closeOptimizationModal = () => {
  showOptimizationModal.value = false
}

const startBatchOptimization = async () => {
  showOptimizationModal.value = false
  showProgressModal.value = true

  optimizationProgress.value = {
    percentage: 0,
    processed: 0,
    total: estimatedFileCount.value,
    currentTask: '准备优化任务...',
    status: '初始化中',
    savedSpace: 0
  }

  // 模拟优化过程
  const tasks = [
    { name: '压缩图片文件', count: 257, savings: 850 * 1024 * 1024 },
    { name: '生成WebP格式', count: 257, savings: 320 * 1024 * 1024 },
    { name: '清理重复文件', count: 156, savings: 280 * 1024 * 1024 },
    { name: '清理缩略图', count: 1247, savings: 45 * 1024 * 1024 }
  ]

  for (const task of tasks) {
    optimizationProgress.value.currentTask = task.name
    optimizationProgress.value.status = '处理中...'

    for (let i = 0; i < task.count; i++) {
      await new Promise(resolve => setTimeout(resolve, 50))
      optimizationProgress.value.processed++
      optimizationProgress.value.percentage = (optimizationProgress.value.processed / optimizationProgress.value.total) * 100
      optimizationProgress.value.savedSpace += task.savings / task.count
    }
  }

  optimizationProgress.value.currentTask = '优化完成'
  optimizationProgress.value.status = '已完成'

  setTimeout(() => {
    showProgressModal.value = false
    Notify({
      type: 'success',
      message: `优化完成，节省空间 ${formatFileSize(optimizationProgress.value.savedSpace)}`,
      position: 'top-right'
    })
  }, 2000)
}

const viewDuplicateGroup = (group: any) => {
  router.push(`/media/duplicates/${group.id}`)
}

const quickCleanDuplicates = async (group: any) => {
  try {
    Notify({
      type: 'info',
      message: '正在清理重复文件...',
      position: 'top-right'
    })

    // 模拟清理过程
    await new Promise(resolve => setTimeout(resolve, 1500))

    // 从列表中移除
    const index = duplicateFiles.value.findIndex(item => item.id === group.id)
    if (index > -1) {
      duplicateFiles.value.splice(index, 1)
    }

    Notify({
      type: 'success',
      message: `已清理 ${group.count - 1} 个重复文件，节省 ${formatFileSize(group.wasteSize)}`,
      position: 'top-right'
    })

  } catch (error) {
    Notify({
      type: 'error',
      message: '清理失败，请重试',
      position: 'top-right'
    })
  }
}

const viewAllDuplicates = () => {
  router.push('/media/duplicates')
}

const applySuggestion = async (suggestion: any) => {
  try {
    Notify({
      type: 'info',
      message: '正在应用优化建议...',
      position: 'top-right'
    })

    // 模拟应用过程
    await new Promise(resolve => setTimeout(resolve, 2000))

    // 从建议列表中移除
    const index = compressionSuggestions.value.findIndex(item => item.id === suggestion.id)
    if (index > -1) {
      compressionSuggestions.value.splice(index, 1)
    }

    Notify({
      type: 'success',
      message: `优化完成，节省空间 ${formatFileSize(suggestion.potentialSavings)}`,
      position: 'top-right'
    })

  } catch (error) {
    Notify({
      type: 'error',
      message: '优化失败，请重试',
      position: 'top-right'
    })
  }
}

const toggleCDN = (enabled: boolean) => {
  Notify({
    type: 'info',
    message: enabled ? 'CDN加速已启用' : 'CDN加速已关闭',
    position: 'top-right'
  })
}

const refreshCDNCache = async () => {
  try {
    Notify({
      type: 'info',
      message: '正在刷新CDN缓存...',
      position: 'top-right'
    })

    await new Promise(resolve => setTimeout(resolve, 2000))

    Notify({
      type: 'success',
      message: 'CDN缓存已刷新',
      position: 'top-right'
    })

  } catch (error) {
    Notify({
      type: 'error',
      message: '刷新失败，请重试',
      position: 'top-right'
    })
  }
}

const preloadCommonFiles = async () => {
  try {
    Notify({
      type: 'info',
      message: '正在预加载热门文件...',
      position: 'top-right'
    })

    await new Promise(resolve => setTimeout(resolve, 3000))

    Notify({
      type: 'success',
      message: '已预加载124个热门文件',
      position: 'top-right'
    })

  } catch (error) {
    Notify({
      type: 'error',
      message: '预加载失败，请重试',
      position: 'top-right'
    })
  }
}

const clearCDNCache = async () => {
  if (!confirm('确定要清空所有CDN缓存吗？这将暂时影响访问速度。')) {
    return
  }

  try {
    Notify({
      type: 'info',
      message: '正在清空CDN缓存...',
      position: 'top-right'
    })

    await new Promise(resolve => setTimeout(resolve, 2000))

    cacheStats.value = {
      ...cacheStats.value,
      totalSize: 0,
      fileCount: 0
    }

    Notify({
      type: 'success',
      message: 'CDN缓存已清空',
      position: 'top-right'
    })

  } catch (error) {
    Notify({
      type: 'error',
      message: '清空失败，请重试',
      position: 'top-right'
    })
  }
}

const updateAnalytics = async () => {
  // 模拟根据时间范围更新分析数据
  await new Promise(resolve => setTimeout(resolve, 500))

  Notify({
    type: 'info',
    message: '访问分析数据已更新',
    position: 'top-right'
  })
}

// 页面初始化
onMounted(() => {
  // 初始化数据加载
  console.log('存储优化页面已初始化')
})
</script>

<style lang="scss" scoped>
@import '@/assets/styles/glassmorphism.scss';

.storage-optimization-view {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
  padding: 2rem 1rem;

  .glass-card {
    @include glass-card;
    margin-bottom: 2rem;
  }

  .page-header {
    max-width: 1400px;
    margin: 0 auto 2rem;

    .header-content {
      background: rgba(255, 255, 255, 0.1);
      backdrop-filter: blur(20px) saturate(180%);
      border: 1px solid rgba(255, 255, 255, 0.2);
      padding: 1.5rem 2rem;
      border-radius: 16px;
      display: grid;
      grid-template-columns: auto 1fr auto;
      align-items: center;
      gap: 2rem;
      box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);

      .header-left {
        .back-btn {
          display: flex;
          align-items: center;
          gap: 0.5rem;
          padding: 0.75rem 1.25rem;
          background: rgba(255, 255, 255, 0.15);
          color: white;
          border: 1px solid rgba(255, 255, 255, 0.2);
          border-radius: 12px;
          cursor: pointer;
          font-weight: 500;
          transition: all 0.3s ease;
          backdrop-filter: blur(10px);

          &:hover {
            background: rgba(255, 255, 255, 0.25);
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
          }

          svg {
            width: 16px;
            height: 16px;
          }
        }
      }

      .header-info {
        text-align: center;

        .page-title {
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 0.75rem;
          font-size: 1.75rem;
          font-weight: 700;
          color: white;
          margin: 0 0 0.5rem 0;
          text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

          svg {
            width: 28px;
            height: 28px;
            color: rgba(255, 255, 255, 0.8);
          }
        }

        .page-description {
          color: rgba(255, 255, 255, 0.8);
          font-size: 0.95rem;
          margin: 0;
          line-height: 1.5;
        }
      }

      .header-actions {
        display: flex;
        gap: 0.75rem;

        .action-btn {
          display: flex;
          align-items: center;
          gap: 0.5rem;
          padding: 0.75rem 1.25rem;
          border-radius: 12px;
          border: 1px solid rgba(255, 255, 255, 0.2);
          cursor: pointer;
          transition: all 0.3s ease;
          font-size: 0.9rem;
          font-weight: 500;
          backdrop-filter: blur(10px);

          svg {
            width: 16px;
            height: 16px;

            &.spinner {
              animation: spin 1s linear infinite;
            }
          }

          &.scan {
            background: rgba(59, 130, 246, 0.2);
            color: white;
            border-color: rgba(59, 130, 246, 0.3);

            &:hover:not(:disabled) {
              background: rgba(59, 130, 246, 0.3);
              transform: translateY(-2px);
              box-shadow: 0 8px 25px rgba(59, 130, 246, 0.25);
            }

            &:disabled {
              opacity: 0.6;
              cursor: not-allowed;
              transform: none;
            }
          }

          &.optimize {
            background: rgba(16, 185, 129, 0.2);
            color: white;
            border-color: rgba(16, 185, 129, 0.3);

            &:hover {
              background: rgba(16, 185, 129, 0.3);
              transform: translateY(-2px);
              box-shadow: 0 8px 25px rgba(16, 185, 129, 0.25);
            }
          }
        }
      }
    }
  }

  .storage-overview {
    max-width: 1400px;
    margin: 0 auto 2rem;
    @include glass-card;
    padding: 2rem;

    .overview-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
      gap: 2rem;

      .overview-item {
        display: flex;
        align-items: center;
        gap: 1.5rem;
        padding: 1.5rem;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 16px;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);

        .item-icon {
          width: 60px;
          height: 60px;
          background: rgba(255, 255, 255, 0.15);
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-shrink: 0;

          svg {
            width: 24px;
            height: 24px;
            color: white;
          }
        }

        .item-content {
          flex: 1;
          min-width: 0;

          h3 {
            color: white;
            font-size: 0.9rem;
            font-weight: 600;
            margin: 0 0 0.75rem 0;
            opacity: 0.9;
          }

          .storage-bar {
            width: 100%;
            height: 6px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 3px;
            overflow: hidden;
            margin-bottom: 0.75rem;

            .storage-used {
              height: 100%;
              background: linear-gradient(90deg, #3b82f6, #10b981);
              border-radius: 3px;
              transition: width 0.5s ease;
            }
          }

          .storage-details {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            color: white;
            font-size: 0.9rem;
            font-weight: 500;

            .used {
              color: #93c5fd;
            }

            .separator {
              opacity: 0.6;
            }

            .total {
              opacity: 0.8;
            }
          }

          .metric-value {
            color: white;
            font-size: 1.5rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
          }

          .metric-trend {
            display: flex;
            align-items: center;
            gap: 0.25rem;
            font-size: 0.8rem;
            font-weight: 500;

            &.up {
              color: #10b981;
            }

            svg {
              width: 12px;
              height: 12px;
            }
          }

          .metric-subtitle {
            color: rgba(255, 255, 255, 0.7);
            font-size: 0.8rem;
          }
        }
      }
    }
  }

  .main-content {
    max-width: 1400px;
    margin: 0 auto;

    .content-grid {
      display: grid;
      grid-template-columns: 2fr 1fr;
      gap: 2rem;

      @media (max-width: 1200px) {
        grid-template-columns: 1fr;
      }
    }
  }

  .analysis-section,
  .cdn-section {
    display: flex;
    flex-direction: column;
    gap: 2rem;
  }

  .storage-distribution,
  .duplicate-detection,
  .compression-suggestions,
  .cdn-status,
  .cache-management,
  .access-analytics {
    @include glass-card;

    .card-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 1.5rem 1.5rem 1rem;
      border-bottom: 1px solid rgba(255, 255, 255, 0.1);

      .card-title {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 1.1rem;
        font-weight: 600;
        color: white;
        margin: 0;

        svg {
          width: 18px;
          height: 18px;
          color: rgba(255, 255, 255, 0.8);
        }
      }

      .card-actions {
        display: flex;
        gap: 0.5rem;

        .refresh-btn {
          width: 32px;
          height: 32px;
          background: rgba(255, 255, 255, 0.1);
          border: 1px solid rgba(255, 255, 255, 0.2);
          border-radius: 8px;
          cursor: pointer;
          color: white;
          display: flex;
          align-items: center;
          justify-content: center;
          transition: all 0.2s ease;

          &:hover {
            background: rgba(255, 255, 255, 0.2);
          }

          svg {
            width: 14px;
            height: 14px;
          }
        }
      }

      .duplicate-count {
        background: rgba(239, 68, 68, 0.2);
        color: #fca5a5;
        padding: 0.25rem 0.75rem;
        border-radius: 12px;
        font-size: 0.8rem;
        font-weight: 500;
      }

      .cdn-toggle {
        .toggle-label {
          display: flex;
          align-items: center;
          gap: 0.5rem;
          color: white;
          font-size: 0.9rem;
          cursor: pointer;

          input[type="checkbox"] {
            position: relative;
            width: 44px;
            height: 24px;
            appearance: none;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 12px;
            cursor: pointer;
            transition: background 0.2s ease;

            &:checked {
              background: #10b981;
            }

            &::before {
              content: '';
              position: absolute;
              top: 2px;
              left: 2px;
              width: 20px;
              height: 20px;
              background: white;
              border-radius: 50%;
              transition: transform 0.2s ease;
            }

            &:checked::before {
              transform: translateX(20px);
            }
          }
        }
      }

      .time-range-selector {
        select {
          padding: 0.5rem 1rem;
          background: rgba(255, 255, 255, 0.1);
          border: 1px solid rgba(255, 255, 255, 0.2);
          border-radius: 8px;
          color: white;
          font-size: 0.9rem;
          cursor: pointer;

          &:focus {
            outline: none;
            border-color: rgba(59, 130, 246, 0.5);
          }

          option {
            background: #374151;
            color: white;
          }
        }
      }
    }

    .card-content {
      padding: 1.5rem;
    }
  }

  .distribution-chart {
    display: flex;
    flex-direction: column;
    gap: 1rem;

    .chart-segment {
      display: flex;
      align-items: center;
      padding: 1rem;
      background: rgba(255, 255, 255, 0.05);
      border-radius: 8px;
      position: relative;
      overflow: hidden;

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: var(--size);
        background: var(--color);
        opacity: 0.3;
      }

      .segment-info {
        position: relative;
        z-index: 1;
        display: flex;
        align-items: center;
        gap: 1rem;
        width: 100%;

        .segment-label {
          color: white;
          font-weight: 500;
          min-width: 60px;
        }

        .segment-size {
          color: rgba(255, 255, 255, 0.8);
          font-size: 0.9rem;
          flex: 1;
        }

        .segment-percentage {
          color: white;
          font-weight: 600;
          font-size: 0.9rem;
        }
      }
    }
  }

  .duplicate-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;

    .duplicate-item {
      display: flex;
      align-items: center;
      gap: 1rem;
      padding: 1rem;
      background: rgba(255, 255, 255, 0.05);
      border-radius: 8px;
      cursor: pointer;
      transition: all 0.2s ease;

      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }

      .duplicate-preview {
        width: 48px;
        height: 48px;
        border-radius: 6px;
        overflow: hidden;
        flex-shrink: 0;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }

        .file-icon {
          width: 100%;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          background: rgba(255, 255, 255, 0.1);
          color: rgba(255, 255, 255, 0.7);

          svg {
            width: 20px;
            height: 20px;
          }
        }
      }

      .duplicate-info {
        flex: 1;
        min-width: 0;

        .duplicate-name {
          color: white;
          font-size: 0.9rem;
          font-weight: 500;
          margin: 0 0 0.25rem 0;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .duplicate-meta {
          display: flex;
          gap: 1rem;
          font-size: 0.8rem;

          .file-count {
            color: rgba(255, 255, 255, 0.7);
          }

          .potential-saving {
            color: #10b981;
            font-weight: 500;
          }
        }
      }

      .duplicate-action {
        .quick-clean-btn {
          width: 32px;
          height: 32px;
          background: rgba(239, 68, 68, 0.1);
          border: none;
          border-radius: 50%;
          color: #fca5a5;
          cursor: pointer;
          display: flex;
          align-items: center;
          justify-content: center;
          transition: all 0.2s ease;

          &:hover {
            background: rgba(239, 68, 68, 0.2);
            transform: scale(1.1);
          }

          svg {
            width: 14px;
            height: 14px;
          }
        }
      }
    }

    .view-all-btn {
      padding: 0.75rem;
      background: rgba(59, 130, 246, 0.1);
      color: #93c5fd;
      border: 1px solid rgba(59, 130, 246, 0.2);
      border-radius: 8px;
      cursor: pointer;
      font-size: 0.9rem;
      font-weight: 500;
      text-align: center;
      transition: all 0.2s ease;

      &:hover {
        background: rgba(59, 130, 246, 0.2);
      }
    }
  }

  .suggestions-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;

    .suggestion-item {
      display: flex;
      align-items: center;
      gap: 1rem;
      padding: 1rem;
      background: rgba(255, 255, 255, 0.05);
      border-radius: 8px;
      border-left: 4px solid transparent;

      &.high {
        border-left-color: #ef4444;
      }

      &.medium {
        border-left-color: #f59e0b;
      }

      &.low {
        border-left-color: #10b981;
      }

      .suggestion-icon {
        width: 40px;
        height: 40px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;

        &.high {
          background: rgba(239, 68, 68, 0.1);
          color: #fca5a5;
        }

        &.medium {
          background: rgba(245, 158, 11, 0.1);
          color: #fcd34d;
        }

        &.low {
          background: rgba(16, 185, 129, 0.1);
          color: #6ee7b7;
        }

        svg {
          width: 18px;
          height: 18px;
        }
      }

      .suggestion-content {
        flex: 1;
        min-width: 0;

        .suggestion-title {
          color: white;
          font-size: 0.95rem;
          font-weight: 600;
          margin: 0 0 0.25rem 0;
        }

        .suggestion-description {
          color: rgba(255, 255, 255, 0.7);
          font-size: 0.85rem;
          margin: 0 0 0.5rem 0;
          line-height: 1.4;
        }

        .suggestion-savings {
          color: #10b981;
          font-size: 0.8rem;
          font-weight: 500;
        }
      }

      .apply-suggestion-btn {
        padding: 0.5rem 1rem;
        background: rgba(59, 130, 246, 0.2);
        color: #93c5fd;
        border: 1px solid rgba(59, 130, 246, 0.3);
        border-radius: 6px;
        cursor: pointer;
        font-size: 0.85rem;
        font-weight: 500;
        flex-shrink: 0;
        transition: all 0.2s ease;

        &:hover {
          background: rgba(59, 130, 246, 0.3);
          transform: translateY(-1px);
        }
      }
    }
  }

  .cdn-nodes {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;

    .node-item {
      display: grid;
      grid-template-columns: 1fr auto auto;
      gap: 1rem;
      padding: 0.75rem;
      background: rgba(255, 255, 255, 0.05);
      border-radius: 8px;
      align-items: center;

      .node-region {
        color: white;
        font-weight: 500;
      }

      .node-latency {
        color: rgba(255, 255, 255, 0.8);
        font-size: 0.9rem;
        text-align: right;
      }

      .node-status {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.8rem;
        font-weight: 500;

        .status-dot {
          width: 8px;
          height: 8px;
          border-radius: 50%;
        }

        &.healthy {
          color: #10b981;

          .status-dot {
            background: #10b981;
          }
        }

        &.warning {
          color: #f59e0b;

          .status-dot {
            background: #f59e0b;
          }
        }

        &.error {
          color: #ef4444;

          .status-dot {
            background: #ef4444;
          }
        }
      }
    }
  }

  .cache-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    margin-bottom: 1.5rem;

    .cache-metric {
      text-align: center;
      padding: 1rem;
      background: rgba(255, 255, 255, 0.05);
      border-radius: 8px;

      .metric-label {
        display: block;
        color: rgba(255, 255, 255, 0.7);
        font-size: 0.8rem;
        margin-bottom: 0.5rem;
      }

      .metric-value {
        color: white;
        font-size: 1.1rem;
        font-weight: 600;
      }
    }
  }

  .cache-actions {
    display: flex;
    gap: 0.75rem;

    .cache-action-btn {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      padding: 0.75rem 1rem;
      background: rgba(255, 255, 255, 0.1);
      color: white;
      border: 1px solid rgba(255, 255, 255, 0.2);
      border-radius: 8px;
      cursor: pointer;
      font-size: 0.85rem;
      font-weight: 500;
      transition: all 0.2s ease;

      &:hover {
        background: rgba(255, 255, 255, 0.15);
      }

      &.danger {
        background: rgba(239, 68, 68, 0.1);
        border-color: rgba(239, 68, 68, 0.2);
        color: #fca5a5;

        &:hover {
          background: rgba(239, 68, 68, 0.2);
        }
      }

      svg {
        width: 14px;
        height: 14px;
      }
    }
  }

  .analytics-summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    margin-bottom: 2rem;

    .summary-item {
      text-align: center;
      padding: 1rem;
      background: rgba(255, 255, 255, 0.05);
      border-radius: 8px;

      .summary-value {
        color: white;
        font-size: 1.2rem;
        font-weight: 700;
        margin-bottom: 0.25rem;
      }

      .summary-label {
        color: rgba(255, 255, 255, 0.7);
        font-size: 0.8rem;
      }
    }
  }

  .top-files {
    h4 {
      color: white;
      font-size: 0.9rem;
      font-weight: 600;
      margin: 0 0 1rem 0;
    }

    .file-list {
      display: flex;
      flex-direction: column;
      gap: 0.5rem;

      .file-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.5rem 0;

        .file-name {
          color: rgba(255, 255, 255, 0.9);
          font-size: 0.85rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          flex: 1;
          margin-right: 1rem;
        }

        .file-requests {
          color: #93c5fd;
          font-size: 0.8rem;
          font-weight: 500;
        }
      }
    }
  }
}

// 模态对话框样式
.optimization-modal,
.progress-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(8px);
}

.modal-content {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;

  .modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.5rem 2rem;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);

    h3 {
      font-size: 1.2rem;
      font-weight: 600;
      color: #1e293b;
      margin: 0;
    }

    .modal-close {
      width: 32px;
      height: 32px;
      background: none;
      border: none;
      border-radius: 8px;
      cursor: pointer;
      color: #64748b;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: all 0.2s ease;

      &:hover {
        background: rgba(0, 0, 0, 0.05);
        color: #1e293b;
      }

      svg {
        width: 16px;
        height: 16px;
      }
    }
  }

  .modal-body {
    padding: 2rem;
    flex: 1;
    overflow-y: auto;
  }

  .modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
    padding: 1.5rem 2rem;
    border-top: 1px solid rgba(0, 0, 0, 0.1);

    .btn {
      padding: 0.75rem 1.5rem;
      border-radius: 8px;
      font-size: 0.9rem;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.2s ease;

      &.btn-secondary {
        background: #f1f5f9;
        color: #64748b;
        border: 1px solid #e2e8f0;

        &:hover {
          background: #e2e8f0;
        }
      }

      &.btn-primary {
        background: #3b82f6;
        color: white;
        border: 1px solid #3b82f6;

        &:hover:not(:disabled) {
          background: #2563eb;
        }

        &:disabled {
          opacity: 0.5;
          cursor: not-allowed;
        }
      }
    }
  }
}

.optimization-options {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  margin-bottom: 2rem;

  .option-group {
    h4 {
      font-size: 1rem;
      font-weight: 600;
      color: #1e293b;
      margin: 0 0 1rem 0;
    }

    .checkbox-label {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      padding: 0.75rem 0;
      cursor: pointer;
      color: #374151;
      font-size: 0.9rem;
      transition: color 0.2s ease;

      &:hover {
        color: #1e293b;
      }

      input[type="checkbox"] {
        width: 18px;
        height: 18px;
        accent-color: #3b82f6;
      }
    }
  }
}

.optimization-preview {
  background: rgba(59, 130, 246, 0.05);
  border: 1px solid rgba(59, 130, 246, 0.1);
  border-radius: 8px;
  padding: 1.5rem;

  .preview-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;

    &:not(:last-child) {
      border-bottom: 1px solid rgba(59, 130, 246, 0.1);
    }

    .preview-label {
      color: #64748b;
      font-size: 0.9rem;
    }

    .preview-value {
      color: #1e293b;
      font-weight: 600;
      font-size: 0.9rem;
    }
  }
}

.progress-content {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  padding: 2rem;
  width: 90%;
  max-width: 500px;

  .progress-header {
    text-align: center;
    margin-bottom: 2rem;

    h3 {
      font-size: 1.2rem;
      font-weight: 600;
      color: #1e293b;
      margin: 0 0 1rem 0;
    }

    .progress-stats {
      display: flex;
      justify-content: space-between;
      color: #64748b;
      font-size: 0.9rem;
    }
  }

  .progress-bar {
    width: 100%;
    height: 8px;
    background: #f1f5f9;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 1.5rem;

    .progress-fill {
      height: 100%;
      background: linear-gradient(90deg, #3b82f6, #10b981);
      border-radius: 4px;
      transition: width 0.3s ease;
    }
  }

  .current-task {
    .task-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.5rem;

      .task-name {
        color: #1e293b;
        font-weight: 500;
      }

      .task-status {
        color: #3b82f6;
        font-size: 0.9rem;
      }
    }

    .task-savings {
      color: #10b981;
      font-size: 0.9rem;
      font-weight: 500;
      text-align: center;
    }
  }
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

// 响应式设计
@media (max-width: 1200px) {
  .storage-optimization-view {
    .main-content .content-grid {
      grid-template-columns: 1fr;
      gap: 1.5rem;
    }

    .overview-grid {
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 1rem;
    }
  }
}

@media (max-width: 768px) {
  .storage-optimization-view {
    padding: 1rem 0.5rem;

    .page-header .header-content {
      grid-template-columns: 1fr;
      text-align: center;
      gap: 1rem;

      .header-actions {
        flex-direction: column;
        gap: 0.5rem;

        .action-btn {
          width: 100%;
          justify-content: center;
        }
      }
    }

    .overview-grid {
      grid-template-columns: 1fr;

      .overview-item {
        flex-direction: column;
        text-align: center;
        gap: 1rem;
      }
    }

    .cache-stats,
    .analytics-summary {
      grid-template-columns: 1fr;
      gap: 0.75rem;
    }

    .cache-actions {
      flex-direction: column;
    }

    .cdn-nodes .node-item {
      grid-template-columns: 1fr;
      gap: 0.5rem;
      text-align: center;
    }
  }

  .modal-content {
    width: 95%;
    margin: 1rem;

    .modal-header,
    .modal-body,
    .modal-footer {
      padding: 1rem;
    }
  }

  .progress-content {
    width: 95%;
    padding: 1.5rem;
  }
}
</style>
